package day7.zy1;
import java.io.*;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;

public class LoginThread extends Thread {
    private final Socket s;

    private static Map<String,String> users =new HashMap<>();
    static {
        users.put("张三","zhangsan");
        users.put("李四","lisi");
        users.put("王五","wangwu");
        users.put("程六","chengliu");
        users.put("小明","xiaoming");
    }

    public LoginThread(Socket s) {
        this.s = s;
    }

    @Override
    public void run() {
        System.out.println("socket建立了"+s);
        try {
            InputStream in =s.getInputStream();
            OutputStream out =s.getOutputStream();
            BufferedReader br =new BufferedReader(new InputStreamReader(in));
            PrintWriter pw =new PrintWriter(out,true);
            String name =br.readLine();
            String pws =br.readLine();
            if (pws.equals(users.get(name))){
                pw.println("登陆成功!");
            }else{
                pw.println("登陆失败!");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                s.close();
                System.out.println(s+"关闭了,聊天结束了");
            } catch (Exception e) {
                //e.printStackTrace();
            }
        }
    }
}
